package com.gwl.concurrent.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.gwl.concurrent.IntGenerator;

public class EvenChecker implements Runnable {

	private IntGenerator generator; 
	public EvenChecker(IntGenerator generator) { 
		this.generator = generator; 
	} 
	
	@Override
	public void run() { 
		while(!generator.isCanceled()) { 
			int i = generator.next(); 
			if(i % 2 != 0) { 
				System.out.println(i + " not even"); 
				generator.canceled(); 
			}
		}
	} 
	
	public static void test(IntGenerator generator, int loopCount) { 
		System.out.println("Press Ctr + C to exit"); 
		ExecutorService exec = Executors.newCachedThreadPool(); 
		for(int i=0; i<loopCount; i++) 
			exec.execute(new EvenChecker(generator)); 
		exec.shutdown(); 
	} 
	public static void test(IntGenerator generator) { 
		test(generator, 10); 
	}

}
